import useUserStore from '@/store/modules/user'
import moment from 'moment'

const enumTimeMap = new Map([
  [0, "zero"],
  [1, "one"],
  [2, "two"],
  [3, "three"],
  [4, "four"],
  [5, "five"],
  [6, "six"],
  [7, "seven"],
  [8, "eight"],
  [9, "nine"],
  [10, "ten"],
  [11, "eleven"],
  [12, "twelve"],
  [13, "thirteen"],
  [14, "fourteen"],
  [15, "fifteen"],
  [16, "sixteen"],
  [17, "seventeen"],
  [18, "eighteen"],
  [19, "nineteen"],
  [20, "twenty"],
  [21, "twentyOne"],
  [22, "twentyTwo"],
  [23, "twentyThree"]
])

const _terget = [
  { headerName: "MBB", field: "productionTarget", width: 76 },
  { headerName: "早班", field: "dy", width: 70 },
  { headerName: "夜班", field: "gy", width: 70 },
  { headerName: "合计", field: "ttl", width: 70 }
]

let _tempTager = null

export const firstTable = [
  ...JSON.parse(JSON.stringify(_terget)),
  { headerName: "8:30", field: "eight", width: 55 },
  { headerName: "9:30", field: "nine", width: 55 },
  { headerName: "10:30", field: "ten", width: 55 },
  { headerName: "11:30", field: "eleven", width: 55 },
  { headerName: "12:30", field: "twelve", width: 55 },
  { headerName: "13:30", field: "thirteen", width: 55 },
  { headerName: "14:30", field: "fourteen", width: 55 },
  { headerName: "15:30", field: "fifteen", width: 55 },
  { headerName: "16:30", field: "sixteen", width: 55 },
  { headerName: "17:30", field: "seventeen", width: 55 },
  { headerName: "18:30", field: "eighteen", width: 55 },
  { headerName: "19:30", field: "nineteen", width: 55 },
  { headerName: "20:30", field: "twenty", width: 55 },
  { headerName: "21:30", field: "twentyOne", width: 55 },
  { headerName: "22:30", field: "twentyTwo", width: 55 },
  { headerName: "23:30", field: "twentyThree", width: 55 },
  { headerName: "0:30", field: "zero", width: 55 },
  { headerName: "1:30", field: "one", width: 55 },
  { headerName: "2:30", field: "two", width: 55 },
  { headerName: "3:30", field: "three", width: 55 },
  { headerName: "4:30", field: "four", width: 55 },
  { headerName: "5:30", field: "five", width: 55 },
  { headerName: "6:30", field: "six", width: 55 },
  { headerName: "7:30", field: "seven", width: 55 }
]

const _pool = ['目标', '产量', '差异']

firstTable.forEach((item) => {
  item.cellRenderer = (params) => {
    if(_pool.includes(params.value)){
      return `${params.value}/万片`
    }
    if(params.value.includes('.')){
      return Number(params.value).toFixed(2)
    } else {
      return params.value
    }
  }

  item.cellStyle = (params) => {
    let _headerName = params.colDef.headerName.slice(0, params.colDef.headerName.length - 3)
    if(params.colDef.field === 'eight' && params.data.productionTarget === '目标'){
      _tempTager = params.value
    }
    if(params.data.productionTarget === '差异' && enumTimeMap.has(Number(_headerName)) && Number(_tempTager) && Number(params.value) + Number(_tempTager) !== 0) {
      if(Number(params.value) > 0 && Number(_tempTager) * 0.05 < Number(params.value)){
        return {
          color: 'blue',
          fontWeight: 700
        }
      } else if (Number(params.value) < 0 && -Number(_tempTager) * 0.05 > Number(params.value)) {
        return {
          color: 'red',
          fontWeight: 700
        }
      }
    }
  }
})

export const getActionColumn = (params, pool = _terget, sum = 28) => {
  params.forEach(element => {
    for (let objKey in element) {
      for(let[key, value] of enumTimeMap) {
        if(objKey === value && pool.length < sum){
          pool.push({
            headerName: `${key}:30`, field: value, width: 55
          })
        }
      }
    }

  })

  return pool
}

export const alarmColumn = [
  { headerName: "报警设备", field: "machineId", minWidth: 100, flex: 1 },
  { headerName: "状态描述", field: "descriptionState", minWidth: 60, flex: 1 },
  { headerName: "开始时间", field: "startTime", minWidth: 120, flex: 1 },
  { headerName: "结束时间", field: "endTime", minWidth: 120, flex: 1 },
  { headerName: "主线损失时间/秒", field: "duration", minWidth: 50, flex: 1 },
  { headerName: "单机读报警损失/秒", field: "readAlarmLoss", minWidth: 50, flex: 1 },
  { headerName: "单机消报警损失/秒", field: "eliminateAlarmLoss", minWidth: 50, flex: 1 },
  { headerName: "报警ID", field: "alarmLossId", minWidth: 50, flex: 1 },
  { headerName: "报警文本", field: "alarmLossText", minWidth: 200, flex: 1 },
  { headerName: "报警源设备", field: "sourceId", minWidth: 100, flex: 1 }
]

export const describeColumn = [
  { headerName: "序号", field: "order", width: 35, cellRenderer: (params) => params.rowIndex + 1 },
  { headerName: "更新时间", field: "updateTime", minWidth: 120, flex: 1 },
  { headerName: "内容", field: "textEditing", minWidth: 1200, flex: 1 },
  { headerName: "创建人", field: "createBy", minWidth: 50, flex: 1 },
  { headerName: "创建时间", field: "createTime", minWidth: 120, flex: 1 }
  // { headerName: "结束时间", field: "endTime", minWidth: 120, flex: 1 },
]

export const editTableColumn = [
  { headerName: "", field: "idx", checkboxSelection: ({ data }) => {
    if(data.textEditing){
      return true
    }
  }, width: 20 },
  // { headerName: "开始时间", field: "startTime", width: 200, /** cellRenderer:timeComponents */ editable: true },
  // { headerName: "结束时间", field: "endTime", width: 200, /** cellRenderer:timeComponents */ editable: true },
  { headerName: "序号", field: "order", width: 35, cellRenderer: (params) => params.rowIndex + 1 },
  { headerName: "内容", field: "textEditing", minWidth: 500, flex: 1, editable: true },
  { headerName: "创建者", field: "createBy", width: 100 },
  { headerName: "创建时间", field: "createTime", width: 150 }
]

export const editTableData = [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}]

/** 赋初始值 */
editTableData.forEach(item => {
  item.createBy = useUserStore().name,
  item.createTime = moment().format('YYYY-MM-DD HH:mm:ss')
})